﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Mail;

using Jzy.Model.Mail;
using Jzy.Model;
using System.Collections.Generic;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Module;
using Frame.Lib.Enums;
using Frame.Lib.Core.Attach;
using Jzy.DAL;


public partial class WebMail_Detail : PageDetail
{
    private WebMailBO _mail;
    private MailFolderBO _mailBox;

    private int FolderID
    {
        get
        {
            return int.Parse(Request["FolderID"]);
        }
    }

    private int MailID
    {
        get
        {
            return int.Parse(Request["ID"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.AllowPageScroll();
        if (FolderID == (int)MailSysFolder.草稿箱)
        {
            Response.Redirect("Edit.aspx?ID=" + MailID + "&Mode=" + (int)WriteMailMode.Draft, true);
        }
        _mail = new WebMailBO(AppSession.UserID);
        _mailBox = new MailFolderBO(AppSession.UserID, MailFolderType.外部邮件);

        if (!IsPostBack)
        {
            BindMenu();
            DispInfo();
            SetRead();
        }
        AddAttr();

        int unreadMailCount = _mail.GetUnreadMailCount();
        ModuleBO module = new ModuleBO();

        object receiveBoxModuleID = module.GetIDByCode(ModuleCode.ReceiveBox);
        if (receiveBoxModuleID != null)
        {
            ModuleInfo info = module.GetInfo(receiveBoxModuleID);
            //SetModuleName(id,name)
            string strScript = "SetModuleName('" + receiveBoxModuleID + "','" + info.Name + "');";
            if (unreadMailCount > 0)
            {
                strScript = "SetModuleName('" + receiveBoxModuleID + "','<span style=\\'font-weight:bold\\'>" + info.Name + "(" + unreadMailCount + ")</span>');";
            }
            JSUtil.ExcuteJS(strScript, "UnreadMail");
        }
    }

    private void AddAttr()
    {
        btnReply.Style.Add("display", "none");
        btnForword.Style.Add("display", "none");
        btnDelete.Style.Add("display", "none");
        btnDelete.Attributes.Add("onclick", "return confirm('确认删除邮件');");
        JSUtil.ClickBtn(lnkReply, btnReply);
        JSUtil.ClickBtn(lnkForward, btnForword);
        JSUtil.ClickBtn(lnkDelete, btnDelete);
    }

    private void SetRead()
    {
        _mail.SetRead(MailID);
    }

    private void DispInfo()
    {        
        WebMailInfo info = _mail.GetInfo(MailID);

        AttachBO attach = new AttachBO(OTableName.WebMailAttach);

        //显示附件
        //if (info.FolderID == (int)MailSysFolder.收件箱 && info.HasDownload == false)
        //{
        //    WebMailAccountBO account = new WebMailAccountBO(AppSession.UserID);
        //    WebMailAccountInfo accountInfo = account.GetInfo(account.GetIDByAddr(info.AccountAddr));
        //    if (accountInfo != null)
        //    {
        //        string body;   
        //        List<AttachInfo> lstAttachs = _mail.DownMail(accountInfo, info.MailGuid,out body);

        //        info.Body = body;
        //        if (lstAttachs.Count > 0)
        //        {
        //            info.HasAttachment = true;
        //        }
        //        info.HasDownload = true;
        //        _mail.Save(info);
        //        info = _mail.GetInfo(MailID);


        //        foreach (AttachInfo attachInfo in lstAttachs)
        //        {
        //            attachInfo.BindID = MailID;
        //            attach.Save(attachInfo);
        //        }                
        //    }
        //}

        EntityUtil.SetControl(pnlMain, info);
        ToAddr.Text = HttpUtility.HtmlEncode(info.ToAddr);
        CCAddr.Text = HttpUtility.HtmlEncode(info.CCAddr);
        if (info.CCAddr == null || info.CCAddr == "")
        {
            trCopyTo.Style["display"] = "none";
        }        
        Content.Text = info.Body;
        
        IEnumerable<AttachInfo> list = attach.GetList(MailID);
        rptAttach.DataSource = list;
        rptAttach.DataBind();

        if (list.Count() == 0)
        {
            trAttach.Style["display"] = "none";
        }
    }
    protected void rptMenu_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        _mail.Move(MailID, e.CommandArgument);
        Response.Redirect("List.aspx?FolderID=" + FolderID, true);
    }

    private void BindMenu()
    {
        List<MailFolderInfo> list = _mailBox.GetAllBoxList();
        rptMenu.DataSource = list;
        rptMenu.DataBind();
    }

    protected void btnReply_Click(object sender, EventArgs e)
    {
        Response.Redirect("Edit.aspx?ID=" + MailID + "&Mode=" + (int)WriteMailMode.Reply, true);
    }
    protected void btnForword_Click(object sender, EventArgs e)
    {
        Response.Redirect("Edit.aspx?ID=" + MailID + "&Mode=" + (int)WriteMailMode.Forword, true);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (FolderID == (int)MailSysFolder.已删除)
        {
            _mail.Delete(MailID);
        }
        else
        {
            _mail.Move(MailID, (int)MailSysFolder.已删除);
        }

        Response.Redirect("List.aspx?FolderID=" + FolderID, true);
    }
}
